package com.celeritymq.connection;

import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import javax.jms.ConnectionConsumer;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.ServerSessionPool;
import javax.jms.Topic;

import com.celeritymq.session.CelerityServerSessionPool;

public class CelerityConnectionConsumer implements ConnectionConsumer {
	private CelerityServerSessionPool pool;
	private int port;
	private Destination destination;
	private String subscriptionName;
	private String messageSelector;
	private int maxMessages;

	public CelerityConnectionConsumer() throws SecurityException, IllegalArgumentException, UnknownHostException,
			NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException,
			InterruptedException {
		port = 9999;
		pool = new CelerityServerSessionPool(new InetSocketAddress(InetAddress.getLocalHost(), port), 10);
	}

	public CelerityConnectionConsumer(SocketAddress address) throws SecurityException, IllegalArgumentException,
			UnknownHostException, NoSuchMethodException, InstantiationException, IllegalAccessException,
			InvocationTargetException, InterruptedException {
		port = 9999;
		pool = new CelerityServerSessionPool(address, 10);
	}

	public CelerityConnectionConsumer(Destination destination, String messageSelector, ServerSessionPool serverSessionPool,
			int maxMessages) {
		this.destination = destination;
		this.messageSelector = messageSelector;
		this.pool = (CelerityServerSessionPool) serverSessionPool;
		this.maxMessages = maxMessages;
	}

	public CelerityConnectionConsumer(Destination destination, String subscriptionName, String messageSelector,
			ServerSessionPool sessionPool, int maxMessages) {
		this.destination = destination;
		this.subscriptionName = subscriptionName;
		this.messageSelector = messageSelector;
		this.pool = (CelerityServerSessionPool) sessionPool;
		this.maxMessages = maxMessages;
	}

	@Override
	public void close() throws JMSException {
		pool.close();
		pool = null;
	}

	@Override
	public ServerSessionPool getServerSessionPool() throws JMSException {
		return pool;
	}

}
